package com.axend.aerosense.pay.ui.adapter;

import android.graphics.Paint;
import android.view.View;

import com.axend.aerosense.common.adapter.BaseBindingAdapter;
import com.axend.aerosense.pay.R;
import com.axend.aerosense.pay.databinding.PayVipMoneyCardItemBinding;
import com.axend.aerosense.pay.entity.VipMoneyCardBean;
import com.blankj.utilcode.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class PayVipMoneyCardAdapter extends BaseBindingAdapter<VipMoneyCardBean, PayVipMoneyCardItemBinding> {

    private boolean isAutoPay = false;
    private int selectItem = 0;
    private VipMoneyCardBean selectItemBean;
    private OnItemClickListener onItemClickListener;
    private List<VipMoneyCardBean> originalPrice;  //原价
    private List<VipMoneyCardBean> favorablePrice;  //优惠价

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.pay_vip_money_card_item;
    }

    @Override
    protected void onBindItem(PayVipMoneyCardItemBinding binding, VipMoneyCardBean item, int position) {
        binding.payTvVipType.setText(getVipTypeStr(item.getVipType()));

        if (!isAutoPay){
            //显示原价
            binding.payTvCurrentPrice.setText(String.valueOf(originalPrice.get(position).getOriginalPrice()));
            //LogUtils.d("原价：" + originalPrice.get(position).getOriginalPrice());
            binding.payTvOriginalPrice.setVisibility(View.GONE);
        }else {
            //勾选自动续费
            binding.payTvOriginalPrice.setVisibility(View.VISIBLE);
            binding.payTvOriginalPrice.setText("$"+String.valueOf(originalPrice.get(position).getDiscountPrice()));
            //设置删除线
            binding.payTvOriginalPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

            binding.payTvCurrentPrice.setText(String.valueOf(favorablePrice.get(position).getOriginalPrice()));
            //LogUtils.d("优惠价：" + favorablePrice.get(position).getOriginalPrice());
        }

        //选中item高亮显示
        if (position == selectItem){
            binding.payMoneyCard.setBackground(getContext().getDrawable(R.drawable.pay_selector_vip_1));
            if (!isAutoPay){
                selectItemBean = originalPrice.get(position);
            }else {
                selectItemBean = favorablePrice.get(position);
            }
        }else {
            binding.payMoneyCard.setBackground(getContext().getDrawable(R.drawable.pay_selector_vip));
        }

        binding.payMoneyCard.setOnClickListener(v -> {
//            if (!isAutoPay){
//                selectItemBean = originalPrice.get(position);
//            }else {
//                selectItemBean = favorablePrice.get(position);
//            }
            if (null != onItemClickListener){
                onItemClickListener.onItemClick(position, selectItemBean);
            }
            selectItem = position;
            notifyDataSetChanged();
        });
    }

    private int getVipTypeStr(int vipType){
        int strId = R.string.pay_vip_months_auto;

        if (vipType == 1 || vipType == 4){
            strId = R.string.pay_vip_months_auto;
        }else if (vipType == 2 || vipType == 5){
            strId = R.string.pay_vip_season_auto;
        }else if (vipType == 3 || vipType == 6){
            strId = R.string.pay_vip_year_auto;
        }
        return strId;
    }

    private void classifiedType(List<VipMoneyCardBean> items){
        originalPrice = new ArrayList<>();
        favorablePrice = new ArrayList<>();
        for (VipMoneyCardBean bean : items){
            int vipType = bean.getVipType();
            if (vipType >= 1 && vipType <= 3){
                originalPrice.add(bean);
            }else if (vipType >= 4 && vipType <= 6){
                favorablePrice.add(bean);
            }
        }
        //LogUtils.d("原价" + originalPrice);
        //LogUtils.d("优惠价" + favorablePrice);
    }

    /**
     * 是否自动续费
     * @param autoPay
     */
    public void setAutoPay(boolean autoPay) {
        isAutoPay = autoPay;
        super.setItems(autoPay ? favorablePrice : originalPrice);
        notifyDataSetChanged();
    }

    public void setData(List<VipMoneyCardBean> data){
        if (null != data && !data.isEmpty()){
            classifiedType(data);
            upDateList(originalPrice);
            selectItemBean = originalPrice.get(0);
        }
    }

    public VipMoneyCardBean getSelectItemBean(){
        return selectItemBean;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position, VipMoneyCardBean item);
    }

}
